/*
 * VistaReservaGuias.java
 *
 * Created on 20 de noviembre de 2008, 04:51 PM
 */

package ddta;

import java.awt.Color;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author  julio
 */
public class VistaReservaGuias extends JDialog {

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,id=DCE.4B503179-C61B-D881-5189-D9FB35585F4A]
    // </editor-fold> 
    private DDTA ddta;
    private DefaultTableModel tableModelGuiasR;
    private DefaultTableModel tableModelGuiasL;

    /** Creates new form VistaReservaGuias */
    public VistaReservaGuias(JDialog parent, boolean modal, DDTA ddta, String codAgencia) {
        super(parent, modal);
        initComponents();
        this.ddta = ddta;
        mostrarAgencia(codAgencia);
        this.tableModelGuiasL = (DefaultTableModel)tblGuiaLibre.getModel();
        this.tableModelGuiasR = (DefaultTableModel)tblGuiaAgencia.getModel();
        tblGuiaLibre.setForeground(new Color(255, 0, 0));
        mostrarGuiasDisponibles();
        mostrarGuiasReservados(codAgencia);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblGuiaLibre = new javax.swing.JTable();
        btnReservar = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblGuiaAgencia = new javax.swing.JTable();
        txtCodAgencia = new javax.swing.JTextField();
        txtAgenciaNombre = new javax.swing.JTextField();
        btnAgregar = new javax.swing.JButton();
        btnLiberar = new javax.swing.JButton();
        btnSalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Guias disponibles"));

        tblGuiaLibre.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Nombre", "Sexo", "Fecha Nac."
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblGuiaLibre);

        btnReservar.setText(" Reservar guia ");
        btnReservar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnReservarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE)
                    .addComponent(btnReservar))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnReservar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Guias de la Agencia"));

        tblGuiaAgencia.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Código", "Nombre", "Sexo", "Fecha Nac."
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane2.setViewportView(tblGuiaAgencia);

        btnAgregar.setText("Registrar nuevo guia");
        btnAgregar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAgregarActionPerformed(evt);
            }
        });

        btnLiberar.setText("Liberar guia");
        btnLiberar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLiberarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(txtCodAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtAgenciaNombre, javax.swing.GroupLayout.DEFAULT_SIZE, 381, Short.MAX_VALUE)
                        .addGap(46, 46, 46))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 506, Short.MAX_VALUE)
                        .addContainerGap())
                    .addGroup(jPanel2Layout.createSequentialGroup()
                        .addComponent(btnLiberar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnAgregar))))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtCodAgencia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtAgenciaNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnAgregar)
                    .addComponent(btnLiberar)))
        );

        btnSalir.setText("   Salir   ");
        btnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnSalir))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSalir)
                .addContainerGap(14, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void btnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalirActionPerformed
// TODO add your handling code here:
    this.setVisible(false);
}//GEN-LAST:event_btnSalirActionPerformed

private void btnReservarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnReservarActionPerformed
// TODO add your handling code here:
    int iRow = tblGuiaLibre.getSelectedRow();
    if (iRow >= 0) {
        String codGuia = this.tableModelGuiasL.getValueAt(iRow, 0).toString();
        GuiaTurista guia = this.ddta.getCtlGuias().buscarGuia(codGuia);
        Agencia agencia = this.ddta.getCtlAgencias().buscarAgencia(txtCodAgencia.getText());
        guia.setAgencia(agencia);
        actualizarGrillas();
    }  else {
        JOptionPane.showMessageDialog(this, "Seleccione un guia");
    }
}//GEN-LAST:event_btnReservarActionPerformed

private void btnLiberarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLiberarActionPerformed
// TODO add your handling code here:
    int iRow = tblGuiaAgencia.getSelectedRow();
    if (iRow >= 0) {
        String codGuia = this.tableModelGuiasR.getValueAt(iRow, 0).toString();
        GuiaTurista guia = this.ddta.getCtlGuias().buscarGuia(codGuia);
        guia.setAgencia(null);
        actualizarGrillas();
    }  else {
        JOptionPane.showMessageDialog(this, "Seleccione un guia");
    }
}//GEN-LAST:event_btnLiberarActionPerformed

private void btnAgregarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAgregarActionPerformed
// TODO add your handling code here:
    VistaGuias fGuia = new VistaGuias(this, true, this.ddta, txtCodAgencia.getText());
    fGuia.setVisible(true);
}//GEN-LAST:event_btnAgregarActionPerformed

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.8F14CB1E-369D-4903-D23C-04334F8C56E8]
    // </editor-fold> 
    public DDTA getDdta () {
        return ddta;
    }

    // <editor-fold defaultstate="collapsed" desc=" UML Marker "> 
    // #[regen=yes,regenBody=yes,id=DCE.748DC137-E6AC-6AA0-A6ED-16E9204546B7]
    // </editor-fold> 
    public void setDdta (DDTA val) {
        this.ddta = val;
    }
    
    private void mostrarAgencia(String codAgencia) {
        Agencia agencia = this.ddta.getCtlAgencias().buscarAgencia(codAgencia);
        txtAgenciaNombre.setText(agencia.getNombreAgencia());
        txtCodAgencia.setText(agencia.getCodigoAgencia());
    }
    private void mostrarGuiasDisponibles() {
        int iCount = this.ddta.getCtlGuias().getGuiasTuristas().size();
        if (iCount > 0) {
            this.tableModelGuiasL.setNumRows(0);
            for (GuiaTurista guia : this.ddta.getCtlGuias().getGuiasTuristas()) {
                if (guia.getAgencia() == null) {
                    this.tableModelGuiasL.addRow(new Object[]{
                        guia.getCodigo(),
                        guia.getNombre(),
                        guia.getSexo(),
                        guia.getFechaNacimiento().getFullDate()
                    });
                }
            }
        }
    }
    private void mostrarGuiasReservados(String codAgencia) {
        int iCount = this.ddta.getCtlGuias().getGuiasTuristas().size();
        if (iCount > 0) {
            Agencia agencia = this.ddta.getCtlAgencias().buscarAgencia(codAgencia);
            this.tableModelGuiasR.setNumRows(0);
            for (GuiaTurista guia : this.ddta.getCtlGuias().getGuiasTuristas()) {
                if (guia.getAgencia() != null && guia.getAgencia().equals(agencia)) {
                    this.tableModelGuiasR.addRow(new Object[]{
                        guia.getCodigo(),
                        guia.getNombre(),
                        guia.getSexo(),
                        guia.getFechaNacimiento().getFullDate()
                    });
                }
            }
        }
    }
    
    public void actualizarGrillas() {
        mostrarGuiasDisponibles();
        mostrarGuiasReservados(txtCodAgencia.getText());
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAgregar;
    private javax.swing.JButton btnLiberar;
    private javax.swing.JButton btnReservar;
    private javax.swing.JButton btnSalir;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblGuiaAgencia;
    private javax.swing.JTable tblGuiaLibre;
    private javax.swing.JTextField txtAgenciaNombre;
    private javax.swing.JTextField txtCodAgencia;
    // End of variables declaration//GEN-END:variables

}
